package org.面试题.Synchronized;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 *  1.给我们的两个同步方法send()和call()加上   static
 *     加上static ，静态方法，类一加载就有了，锁得是person3的唯一的class模板，只有一个class对象
 *   2.在new一个person对象，依然还是静态方法,当然还是锁的person的惟一的class模板，还是先发消息后打电话
 */
public class synchronizedDemo3 {
    public static void main(String[] args) throws InterruptedException {
        Person3 person3 = new Person3();
        Person3 person4 = new Person3();
        new Thread(()->{
            person3.send();
        },"A").start();

     TimeUnit.SECONDS.sleep(3);
//        new Thread(()->{
//            person3.call();
//        },"B").start();
//        TimeUnit.SECONDS.sleep(3);
        new Thread(()->{
            person4.call();
        },"B").start();
    }
}
class Person3{
    public static synchronized void send(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"发消息");
    }

    public static synchronized void call(){
        System.out.println(Thread.currentThread().getName()+"打电话");
    }
    public   void eat(){
        System.out.println(Thread.currentThread().getName()+"干饭");
    }
}
